﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using be.belgium.eid;
using System.Globalization;
using Toolkit;
using DCMSExceptions;

namespace Presentation {
  /// <summary>
  /// eId card reader
  /// </summary>
  public class CardReader {

    /// <summary>
    /// Reads the card.
    /// </summary>
    /// <returns></returns>
    public static BasicCardData ReadCard( ) {
      BasicCardData basicData = null;

      try {
        BEID_ReaderSet.releaseSDK( );

        BEID_ReaderContext readerContext = BEID_ReaderSet.instance( ).getReader( );

        if ( readerContext.getCardType( ) == BEID_CardType.BEID_CARDTYPE_EID
           || readerContext.getCardType( ) == BEID_CardType.BEID_CARDTYPE_FOREIGNER
           || readerContext.getCardType( ) == BEID_CardType.BEID_CARDTYPE_KIDS ) {

          basicData = ReadEid( readerContext );
        }

      } catch ( BEID_ExNoReader ) {
        throw;
      } catch ( BEID_ExNoCardPresent ) {
        throw;
      } catch ( TypeInitializationException ) {
        throw new BusinessException( "Fout bij kaartlezen." );
      } finally {
        try {
          BEID_ReaderSet.releaseSDK( );
        } catch ( Exception ) {
          throw new BusinessException( "Fout bij kaartlezen." );
        }
      }
      return basicData;
    }

    private static BasicCardData ReadEid( BEID_ReaderContext readerContext ) {
      BasicCardData basicData = new BasicCardData( );

      BEID_EIDCard card = readerContext.getEIDCard( );
      BEID_EId idDocument = card.getID( );
      BEID_Picture picture = card.getPicture( );

      basicData.FirstName = idDocument.getFirstName( );
      basicData.Birthday = DateTime.Parse( idDocument.getDateOfBirth( ) );
      basicData.Name = idDocument.getSurname( );
      basicData.Street = idDocument.getStreet( );
      basicData.Zipcode = idDocument.getZipCode( );
      basicData.City = idDocument.getMunicipality( );
      basicData.CountryCode = idDocument.getCountry( ).ToUpper( CultureInfo.CurrentCulture );
      basicData.NationalNumber = idDocument.getNationalNumber( );
      basicData.Photo = GraphicTools.Convert.ByteArrayToImage( picture.getData( ).GetBytes( ) );

      return basicData;
    }


  }

  /// <summary>
  /// Basic eId card data
  /// </summary>
  public class BasicCardData {

    private string name, firstName, street, zipcode, city, countryCode, nationalNumber;
    private DateTime birthday;
    private Image photo;

    #region - Properties -

    /// <summary>
    /// Gets or sets the birthday.
    /// </summary>
    /// <value>
    /// The birthday.
    /// </value>
    public DateTime Birthday {
      get { return birthday; }
      set { birthday = value; }
    }

    /// <summary>
    /// Gets or sets the photo.
    /// </summary>
    /// <value>
    /// The photo.
    /// </value>
    public Image Photo {
      get { return photo; }
      set { photo = value; }
    }

    /// <summary>
    /// Gets or sets the national number.
    /// </summary>
    /// <value>
    /// The national number.
    /// </value>
    public string NationalNumber {
      get { return nationalNumber; }
      set { nationalNumber = value; }
    }

    /// <summary>
    /// Gets or sets the country code.
    /// </summary>
    /// <value>
    /// The country code.
    /// </value>
    public string CountryCode {
      get { return countryCode; }
      set { countryCode = value; }
    }

    /// <summary>
    /// Gets the country id.
    /// </summary>
    public int CountryId {
      get {
        if ( CountryCode == "BE" ) {
          return 1;
        } else {
          return 2;
        }
      }
    }

    /// <summary>
    /// Gets or sets the zipcode.
    /// </summary>
    /// <value>
    /// The zipcode.
    /// </value>
    public string Zipcode {
      get { return zipcode; }
      set { zipcode = value; }
    }

    /// <summary>
    /// Gets or sets the city.
    /// </summary>
    /// <value>
    /// The city.
    /// </value>
    public string City {
      get { return city; }
      set { city = value; }
    }

    /// <summary>
    /// Gets or sets the street.
    /// </summary>
    /// <value>
    /// The street.
    /// </value>
    public string Street {
      get { return street; }
      set { street = value; }
    }

    /// <summary>
    /// Gets or sets the first name.
    /// </summary>
    /// <value>
    /// The first name.
    /// </value>
    public string FirstName {
      get { return firstName; }
      set { firstName = value; }
    }

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    /// <value>
    /// The name.
    /// </value>
    public string Name {
      get { return name; }
      set { name = value; }
    }

    #endregion


  }
}
